app.controller('RentModuleCtrl', function ($scope, ajax, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.list = [];
	//查询
	$scope.reload = function () {
		ajax.call(new Req.GET({
			resource: 'config/module/rt'
		})).then(function (result) {
			$scope.list = result.result;
			for (var i = 0; i < $scope.list.length; i++) {
				var key = $scope.list[i].key;
				if (key == "rent.order.days") {
					$scope.days = $scope.list[i]; //约租下单日期限制
				} else if (key == "rent.timeout.pay") {
					$scope.pay = $scope.list[i];//约租支付超时时间
				} else if (key == "rent.order.points") {
					$scope.points = $scope.list[i];//约租订单积分规则
				} else if (key == "rent.shift.advance") {
					$scope.advance = $scope.list[i]; //提前排班时间
				} else if (key == "rent.shift.time") {
					$scope.time = $scope.list[i];  //排班时间段
				} else if (key == "rent.order.times") {
					$scope.times = $scope.list[i];  //约租下单时间限制
				} else if (key == "rent.order.limit") {
					$scope.limit = $scope.list[i];  //约租预约限制
				} else if (key == "rent.service.protocol") {
					$scope.protocol = $scope.list[i];  //约租服务协议
				} else if (key == "rent.insurance.inform") {
					$scope.ins = $scope.list[i];//约租保险协议
				} else if (key == "rent.refund.rule") {
					$scope.rule = $scope.list[i];//退款手续费规则
				} else if (key == "rent.service.info"){
					$scope.info=$scope.list[i]; //温馨提醒
				}
			}
		}, function () { });
	};

	$scope.reload();

	//保存修改
	$scope.save = function (module) {
		if(module.value==undefined){
			toaster.pop('warning', '提示', '填写格式错误或内容长度超出，请修改后再提交！');
			return;
		}
		updater.fields = module;
		updater.id = module.id;
		updater.resource = 'config/module/bk/update';
		ajax.call(updater).then(function (result) {
			if (result.code == 200) {
				$scope.reload();
				toaster.pop('success', '成功', '修改信息成功。');
			} else {
				toaster.pop('error', '错误', '修改信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '修改信息失败！');
		});
	};

	//恢复默认值
	$scope.reset = function (module) {
		inserter.resource = 'config/module/reset?key=' + module.key;
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				$scope.reload();
				toaster.pop('success', '成功', '恢复默认值成功。');
			} else {
				toaster.pop('error', '错误', '恢复默认失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '恢复默认失败！');
		});
	};
});
